public with sharing class ctrl_ContactFullInformation
{
    
    //For Variables...
    public ApexPages.StandardController controllerProperty {get;set;}
    public Contact contactObject {get;set;}
    public Passport_History__c passportObject {get;set;}
    public Visa_History__c visaObject {get;set;}
    public string objectName = null; 
    public Boolean currentEmployerRenderingFlag {get;set;}
    public string trackingParameterId {get;set;}
    //For Contructor... 
    public ctrl_ContactFullInformation(ApexPages.StandardController controller)
    {
        //Assign the Controller to the Property...
        controllerProperty  = controller;
        currentEmployerRenderingFlag = true;
        //Initializing the Objects...
        contactObject = new Contact();
        passportObject =  new Passport_History__c();
        visaObject = new Visa_History__c();
        
        //For Tracking Contact Creation Button...
        if(ApexPages.CurrentPage().getParameters().get('objectName') != null)
        {
        	objectName = ApexPages.CurrentPage().getParameters().get('objectName');
        	currentEmployerRenderingFlag = false;
        }
        if(ApexPages.CurrentPage().getParameters().get('trackingId') != null)
        {
        	trackingParameterId = ApexPages.CurrentPage().getParameters().get('trackingId');
        	currentEmployerRenderingFlag = false;
        }
        
        
        
    }
    
    //For Save Button...
    public PageReference saveButtonMethod()
    {
        
        Savepoint sp = Database.setSavepoint();
        
        try
        {
            
            //First Insert the Contact...
            Account masterAccountObject = new Account();
            masterAccountObject = [select id from Account where Master_Account_Flag__c =: true LIMIT 1];
            
            //Master Record Id...
            contactObject.AccountId =  masterAccountObject.id;
            
            //Now Update the Tracking Object (Application Name and Passport Number)
            Tracking__c trackingObject = null;
            if(objectName != null)
            {
            	//trackingObject = [select id, Transaction_Type_Id_Formula_Field__c,Company_Name__c,Applicant_Name_Lookup__c,Applicant_Passport_Lookup__c FROM Tracking__c where Id =: Apexpages.CurrentPage().getParameters().get('id')];
            	trackingObject = [select id, Transaction_Type_Id_Formula_Field__c,Company_Name__c,Applicant_Name_Lookup__c,Applicant_Passport_Lookup__c FROM Tracking__c where Id =: trackingParameterId];
				 
	            System.debug('The Transaction Type Id => '+ trackingObject.Transaction_Type_Id_Formula_Field__c);
	            if(trackingObject.Transaction_Type_Id_Formula_Field__c == 'Passport - Passport In')
	            	contactObject.Passport_Movement_Status__c = 'In';
	            else if (trackingObject.Transaction_Type_Id_Formula_Field__c == 'Passport - Passport Out')	
	            	contactObject.Passport_Movement_Status__c = 'Out';
	            //Tracking__c selectedTrackingObject =(Tracking__c)controllerProperty.getRecord();
	            //System.debug('The Selected Tracking Object => '+ selectedTrackingObject.Company_Name__c);
	            
	            contactObject.Current_Employer__c = trackingObject.Company_Name__c;
	            System.debug('The Current Employer => '+ contactObject.Current_Employer__c);
            }
            
            //Now Inserting the 
            insert contactObject;
            System.debug('The After Current Employer => '+ contactObject.Current_Employer__c);
            
            //Now Inserting the Passport History Number and Visa...(Populating the Contact Ids in the Visa History and Passport History)
            passportObject.Passport_Holder__c = contactObject.Id;
            passportObject.Place_of_Birth__c = contactObject.Birth_Place__c;
            passportObject.Place_of_Birth_Arabic__c = contactObject.Birth_Place_Arabic__c;
            passportObject.isCurrent__c = true;            
            
            //Now Inserting the Passport Histories...
            insert passportObject;
            
            //For Visa History 
            if(objectName != null)
            {
            	if(trackingObject.Transaction_Type_Id_Formula_Field__c == 'Letter - Lost Labor Card' || trackingObject.Transaction_Type_Id_Formula_Field__c == 'Transfer - Employee - From AFZA to Government Entity' || trackingObject.Transaction_Type_Id_Formula_Field__c == 'Transfer - Employee - From Government Entity to AFZA' || trackingObject.Transaction_Type_Id_Formula_Field__c == 'Transfer - Employee - Within AFZA' || trackingObject.Transaction_Type_Id_Formula_Field__c == 'Transfer - Investor - From AFZA to Government Entity' || trackingObject.Transaction_Type_Id_Formula_Field__c == 'Transfer - Investor - From Government Entity to AFZA' || trackingObject.Transaction_Type_Id_Formula_Field__c == 'Transfer - Investor - Within AFZA' || trackingObject.Transaction_Type_Id_Formula_Field__c == 'Amendment - Normal - Occupation Change' || trackingObject.Transaction_Type_Id_Formula_Field__c == 'Amendment - Normal - Transfer to New Passport' || trackingObject.Transaction_Type_Id_Formula_Field__c == 'Amendment - Urgent - Occupation Change' || trackingObject.Transaction_Type_Id_Formula_Field__c == 'Amendment - Urgent - Transfer to New Passport')
	            {
	            	visaObject.Visa_Package__c = 'Employment';
	            	visaObject.Visa_Type__c = 'Residency Permit';
	            }
	            else if (trackingObject.Transaction_Type_Id_Formula_Field__c == 'Cancellation - Unused Entry Permit')
	            {
	            	visaObject.Visa_Package__c = 'Employment';
	            	visaObject.Visa_Type__c = 'Entry Permit';
	            }
	            else if(trackingObject.Transaction_Type_Id_Formula_Field__c == 'Visit Visa - 30 days')
	            {
	            	visaObject.Visa_Package__c = 'Visit Visa';
	            	visaObject.Visa_Type__c = 'Short Term Visit Visa';
	            	
	            }	
	            else if(trackingObject.Transaction_Type_Id_Formula_Field__c == 'Visit Visa - 90 Days')
	            {
	            	visaObject.Visa_Package__c = 'Visit Visa';
	            	visaObject.Visa_Type__c = 'Long Term Visit Visa';
	            }
	            else if( trackingObject.Transaction_Type_Id_Formula_Field__c == 'Cancellation - Unused visit Visa')
	            {
	            	visaObject.Visa_Package__c = 'Visit Visa';            	
	            }
            }
	        
	        //Dont need to insert in the Contact Pages...
	        if(objectName != null)
            {
            	//Visa Holder Contact ....
	            visaObject.Visa_Holder__c = contactObject.Id;
	            visaObject.Sponsor__c = contactObject.Current_Employer__c;
	            //Now Inserting the Visa History and Updating the .... (Populating the Id for the Passport Number...)
	            //For Visa History 
	            visaObject.Passport_History_Number__c = passportObject.Id;
	            visaObject.isCurrent__c = true;
	            
	            //Now inserting the Visa History...
	            insert visaObject;
            }
	        
            
            //***************** Old Approach when the isCurrent Passport and Visa was not Used...    
            //Now Updating the Contact .... (Populating the Id for the Passport Number and Visa Reference Number...)
            //Put the Passport History Id in Contact...
            //contactObject.Passport_Number__c = passportObject.Id;
                
            //Put the Visa History Id in Contact...
            //contactObject.Visa_Reference_Number__c = visaObject.id;
                
            //Now Update the contact...
            //update contactObject;
            
            //******************** End Old Approach ****************
            if(objectName != null)
            {
            	if(trackingObject != null)
	            {
	                trackingObject.Applicant_Name_Lookup__c = contactObject.id;
	                trackingObject.Applicant_Passport_Lookup__c  = passportObject.Id;
	                
	                //Now Update the Tracking Object...
	                update trackingObject;
	                    
	            }	
            }
            
            
            PageReference pageRef = null;                
            //Redirect user to the Tracking page...
            if(objectName != null)
            {
            	//pageRef = new PageReference('/'+ Apexpages.CurrentPage().getParameters().get('id'));
            	pageRef = new PageReference('/'+ trackingParameterId);
            	
            }
	            
            else 
            	pageRef = new PageReference('/'+ contactObject.Id);

            //Return the Values...
            return pageRef;
            
        }
        catch(Exception exp)
        {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'The Error => '+ exp.getMessage());
            ApexPages.addMessage(msg);
            Database.rollback(sp);
            
            //Clone the object incase we get the error...
            contactObject = contactObject.clone(false);
            passportObject = passportObject.clone(false);
            visaObject = visaObject.clone(false);
            
            //Now returning the values...
            return null;
            
        }
        
        
    }
    
    //For Cancel Button Method...
    public PageReference cancelPageMethod()
    {
    	PageReference pageRef = null; 
    	//For tracking object...
        if(objectName != null)
        {
        	//pageRef = new PageReference('/'+ApexPages.CurrentPage().getParameters().get('Id'));
        	pageRef = new PageReference('/'+trackingParameterId);
        }
        	
        else 
        	pageRef = new PageReference('/003/o');
        
        return pageRef;
        
    }
    

}